package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.business.domain.AuthBindingInfo;
import com.ruoyi.business.domain.OperationRemind;
import com.ruoyi.business.domain.WxUser;
import com.ruoyi.business.service.IAuthBindingInfoService;
import com.ruoyi.business.service.IOperationRemindService;
import com.ruoyi.business.service.IWxUserService;
import com.ruoyi.business.vo.OperationRemindType;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WxNoticeMapper;
import com.ruoyi.business.domain.WxNotice;
import com.ruoyi.business.service.IWxNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 微信通知公告Service业务层处理
 * 
 * @author asher
 * @date 2021-11-12
 */
@Service
public class WxNoticeServiceImpl implements IWxNoticeService 
{
    @Autowired
    private WxNoticeMapper wxNoticeMapper;


    @Autowired
    private IAuthBindingInfoService authBindingInfoService;

    @Autowired
    private IOperationRemindService operationRemindService;

    /**
     * 查询微信通知公告
     * 
     * @param noticeId 微信通知公告主键
     * @return 微信通知公告
     */
    @Override
    public WxNotice selectWxNoticeByNoticeId(Integer noticeId)
    {
        return wxNoticeMapper.selectWxNoticeByNoticeId(noticeId);
    }

    /**
     * 查询微信通知公告列表
     * 
     * @param wxNotice 微信通知公告
     * @return 微信通知公告
     */
    @Override
    public List<WxNotice> selectWxNoticeList(WxNotice wxNotice)
    {
        return wxNoticeMapper.selectWxNoticeList(wxNotice);
    }

    /**
     * 新增微信通知公告
     * 
     * @param wxNotice 微信通知公告
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWxNotice(WxNotice wxNotice)
    {
        wxNotice.setCreateTime(DateUtils.getNowDate());
        int i = wxNoticeMapper.insertWxNotice(wxNotice);
        AuthBindingInfo authBindingInfo = new AuthBindingInfo();
        authBindingInfo.setVillageId(wxNotice.getVillageId());
        authBindingInfo.setStatus("2");
        List<AuthBindingInfo> authBindingInfos = authBindingInfoService.selectAuthBindingInfoList(authBindingInfo);
        if (authBindingInfos.isEmpty()) {
            return i;
        }
        List<OperationRemind> list = new ArrayList<>();
        for (AuthBindingInfo item : authBindingInfos) {
            list.add(new OperationRemind(OperationRemindType.NOTICE, "[最新公告]您有一条新的公告信息尚未读取!", Long.valueOf(wxNotice.getNoticeId()), "0",
                    item.getWxUserId(), item.getVillageId(), DateUtils.getNowDate(), wxNotice.getCreateBy()));
        }
        operationRemindService.batchOperationRemind(list);
        return i;
    }

    /**
     * 修改微信通知公告
     * 
     * @param wxNotice 微信通知公告
     * @return 结果
     */
    @Override
    public int updateWxNotice(WxNotice wxNotice)
    {
        wxNotice.setUpdateTime(DateUtils.getNowDate());
        return wxNoticeMapper.updateWxNotice(wxNotice);
    }

    /**
     * 批量删除微信通知公告
     * 
     * @param noticeIds 需要删除的微信通知公告主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWxNoticeByNoticeIds(Integer[] noticeIds)
    {
        ArrayList<Integer> integers = new ArrayList<>(Arrays.asList(noticeIds));
        for (Integer item : integers) {
            operationRemindService.deleteOperationRemindByBusinessId(Long.valueOf(item));
        }
        return wxNoticeMapper.deleteWxNoticeByNoticeIds(noticeIds);
    }

    /**
     * 删除微信通知公告信息
     * 
     * @param noticeId 微信通知公告主键
     * @return 结果
     */
    @Override
    public int deleteWxNoticeByNoticeId(Integer noticeId)
    {
        operationRemindService.deleteOperationRemindByBusinessId(Long.valueOf(noticeId));
        return wxNoticeMapper.deleteWxNoticeByNoticeId(noticeId);
    }
}
